<?php

namespace plugin\ky_black\app\admin\controller;

use support\Request;
use support\Response;
use plugin\ky_black\app\admin\model\Service;

class ServiceController
{
    /**
     * 获取服务列表
     */
    public function list(Request $request)
    {
        $query = Service::query();
        
        // 关键词搜索
        if ($keyword = $request->input('keyword')) {
            $query->where('name', 'like', "%{$keyword}%")
                  ->orWhere('service_no', 'like', "%{$keyword}%");
        }
        
        // 服务类型筛选
        if ($type = $request->input('service_type')) {
            $query->where('service_type', $type);
        }
        
        // 服务级别筛选
        if ($level = $request->input('service_level')) {
            $query->where('service_level', $level);
        }
        
        // 状态筛选
        if ($status = $request->input('status')) {
            $query->where('status', $status);
        }
        
        $list = $query->orderBy('created_at', 'desc')
            ->paginate($request->input('limit', 15));
            
        return json_success('获取成功', $list);
    }

    /**
     * 获取选项数据
     */
    public function options()
    {
        $data = [
            'business' => [], // 从商家表获取数据
        ];
        
        return json_success('获取成功', $data);
    }

    /**
     * 添加服务
     */
    public function add(Request $request)
    {
        $data = $request->post();
        
        $service = new Service();
        $service->name = $data['name'];
        $service->service_no = $data['service_no'];
        $service->service_type = $data['service_type'];
        $service->service_level = $data['service_level'];
        $service->phone = $data['phone'];
        $service->email = $data['email'];
        $service->business_id = $data['business_id'];
        $service->backup_phone = $data['backup_phone'] ?? '';
        $service->description = $data['description'] ?? '';
        $service->avatar = $data['avatar'] ?? '';
        $service->status = $data['status'] ?? 1;
        
        if ($service->save()) {
            return json_success('添加成功');
        }
        return json_error('添加失败');
    }

    /**
     * 编辑服务
     */
    public function edit(Request $request)
    {
        $data = $request->post();
        
        $service = Service::find($data['id']);
        if (!$service) {
            return json_error('服务不存在');
        }
        
        $service->name = $data['name'];
        $service->service_no = $data['service_no'];
        $service->service_type = $data['service_type'];
        $service->service_level = $data['service_level'];
        $service->phone = $data['phone'];
        $service->email = $data['email'];
        $service->business_id = $data['business_id'];
        $service->backup_phone = $data['backup_phone'] ?? $service->backup_phone;
        $service->description = $data['description'] ?? $service->description;
        $service->avatar = $data['avatar'] ?? $service->avatar;
        $service->status = $data['status'] ?? $service->status;
        
        if ($service->save()) {
            return json_success('编辑成功');
        }
        return json_error('编辑失败');
    }

    /**
     * 删除服务
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');
        
        if (Service::destroy($id)) {
            return json_success('删除成功');
        }
        return json_error('删除失败');
    }
} 